package fhp.hlhj.giantfold.activity

import android.view.LayoutInflater
import android.view.ViewGroup
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.adapter.AccountDetailListAdp
import fhp.hlhj.giantfold.adapter.AccountGainlListAdp
import fhp.hlhj.giantfold.customView.GainPop
import fhp.hlhj.giantfold.interfaces.IGainDetail
import fhp.hlhj.giantfold.javaBean.CashDetailBean
import fhp.hlhj.giantfold.javaBean.GainDetailBean
import fhp.hlhj.giantfold.presenter.AccountDetailPresenter
import kotlinx.android.synthetic.main.activity_gain_detail_aty.*
import kotlinx.android.synthetic.main.empty_view.*
import java.text.DecimalFormat
import java.util.ArrayList

class GainDetailAty : BaseActivity(), GainPop.GainPopListener, IGainDetail, SpringView.OnFreshListener {
    private lateinit var gainPop:GainPop
    override fun getContentId(): Int = R.layout.activity_gain_detail_aty
    private lateinit var detailListPresenter:AccountDetailPresenter
    private lateinit var getCashAdp:AccountDetailListAdp
    private lateinit var gainAdp:AccountGainlListAdp
    private var page=0
    private var type=0
    private val getCashDatas =ArrayList<CashDetailBean.DataBean>()
    private val gainDatas =ArrayList<GainDetailBean.DataBean>()

    override fun initView() {
        gainPop= GainPop(this,this)
        detailListPresenter= AccountDetailPresenter(this)

        listView.emptyView= empty_view
        spView.footer=DefaultFooter(this)
        spView.header=DefaultHeader(this)
        spView.setListener(this)

        gain()
    }
    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
        btChooseType.setOnClickListener {
            if (gainPop.isShowing){
                gainPop.dismiss()
            }else{
                gainPop.showAsDropDown(btChooseType)
            }
        }
    }
    /**
     * pop监听
     */
    override fun available() {
        type=0;
        btChooseType.text = "余额明细"
        tv1.text="余额（元）"
        page=0
        gainDatas.clear()
        gainAdp=AccountGainlListAdp(this,gainDatas)
        listView.adapter=gainAdp

        val params = HttpParams()
        params.put("page",page)
        params.put("user_id",Contents.userId)
        detailListPresenter.getAvaDetail(params)
    }

    override fun gain() {
        type=1;
        btChooseType.text = "收益明细"
        tv1.text="累计收益（元）"
        page=0
        gainDatas.clear()
        gainAdp=AccountGainlListAdp(this,gainDatas)
        listView.adapter=gainAdp
        val params = HttpParams()
        params.put("page",page)
        params.put("user_id",Contents.userId)
        detailListPresenter.getGainDetail(params)
    }

    override fun spend() {
        type=2;
        btChooseType.text = "消费总额明细"
        tv1.text="累计消费（元）"
        page=0
        gainDatas.clear()
        gainAdp=AccountGainlListAdp(this,gainDatas)
        listView.adapter=gainAdp
        val params = HttpParams()
        params.put("page",page)
        params.put("user_id",Contents.userId)
        detailListPresenter.getSpendDetail(params)
    }

    override fun getcash() {
        type=3;
        getCashAdp = AccountDetailListAdp(this, getCashDatas)
        listView.adapter= getCashAdp
        btChooseType.text = "提现明细"
        tv1.text="累计提现（元）"
        getCashDatas.clear()
        val params = HttpParams()
        params.put("user_id",Contents.userId)
        params.put("page",page)
        detailListPresenter.getAccountDetail(params)
    }

    /**
     * 接口
     */
    override fun showLoading() {
        loadingDialog.show()
    }

    override fun getDetailList(getcash: CashDetailBean) {
        loadingDialog.dismiss()
        spView.onFinishFreshAndLoad()
        if (getcash.code==200){
            val format = DecimalFormat("0.000")

            totalGain.text=format.format(getcash.allMoney.toString().toDouble()).toString()
            getCashDatas.addAll(getcash.data)
            getCashAdp.notifyDataSetChanged()
        }
    }

    override fun getGainDetailList(gainDetailBean: GainDetailBean) {
        loadingDialog.dismiss()
        spView.onFinishFreshAndLoad()
        if (gainDetailBean.code==200){
            val format = DecimalFormat("0.000")
            totalGain.text=format.format(gainDetailBean.earnings_accumulated.toString().toDouble()).toString()
            gainDatas.addAll(gainDetailBean.data)
            gainAdp.notifyDataSetChanged()
        }
    }

    /**
     * 拉监听
     */
    override fun onLoadmore() {
        page++

        when(type){
            0->{
                val params = HttpParams()
                params.put("page",page)
                params.put("user_id",Contents.userId)
                detailListPresenter.getAvaDetail(params)
            }
            1->{
                val params = HttpParams()
                params.put("page",page)
                params.put("user_id",Contents.userId)
                detailListPresenter.getGainDetail(params)
            }
            2->{
                val params = HttpParams()
                params.put("page",page)
                params.put("user_id",Contents.userId)
                detailListPresenter.getSpendDetail(params)
            }
            3->{
                val params = HttpParams()
                params.put("page",page)
                params.put("user_id",Contents.userId)
                detailListPresenter.getAccountDetail(params)
            }
        }
    }

    override fun onRefresh() {
        page=0
        gainDatas.clear()
        getCashDatas.clear()
        when(type){
            0->{
                val params = HttpParams()
                params.put("page",page)
                params.put("user_id",Contents.userId)
                detailListPresenter.getAvaDetail(params)
            }
            1->{
                val params = HttpParams()
                params.put("page",page)
                params.put("user_id",Contents.userId)
                detailListPresenter.getGainDetail(params)
            }
            2->{
                val params = HttpParams()
                params.put("page",page)
                params.put("user_id",Contents.userId)
                detailListPresenter.getSpendDetail(params)
            }
            3->{
                val params = HttpParams()
                params.put("page",page)
                params.put("user_id",Contents.userId)
                detailListPresenter.getAccountDetail(params)
            }
        }
    }
}
